//
//  ViewController.m
//  TB
//
//  Created by City--Online on 15/11/6.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"

#import "BuyBottomView.h"
#import "BuyTopImageCell.h"
#import "DetailViewCell.h"
#import "DViewController.h"

#import "BuyView.h"
#import "BottomCell.h"


#define screenH [UIScreen mainScreen].bounds.size.height
#define screenW [UIScreen mainScreen].bounds.size.width
#define Time 0.3
#define ViewHeight  60
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,JXRollViewDelegate>
{
    NSArray *arrUrls;
}

@property (weak, nonatomic) BuyBottomView* bottomView;
@property (weak, nonatomic) BuyTopImageCell* imageView;
@property (weak, nonatomic) BuyView* buyView;
@property (nonatomic,strong) UIScrollView *scrollView;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) DViewController *vc1;

@property (nonatomic, strong)JXRollView *rollViewColor;

@property (nonatomic, strong)UIImageView *imgView;

@property (nonatomic, strong)UIView *viewBigImage;
@property (nonatomic, strong)UIView *viewImageBack;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
    _scrollView.contentSize=CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
    
    _scrollView.delegate=self;
    _scrollView.pagingEnabled=YES;
    [self.view addSubview:_scrollView];
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH-ViewHeight) style:UITableViewStylePlain];
    
    _tableView.delegate=self;
    _tableView.dataSource=self;

   
    
    [_scrollView addSubview:_tableView];
    
    UIView *vvv = [[UIView alloc]initWithFrame:CGRectMake(0, screenH-ViewHeight, screenW, ViewHeight)];
    [self.view addSubview:vvv];
    
    BuyView* buyVieww = [BuyView view];
    
    self.buyView = buyVieww;
    [vvv addSubview:self.buyView];
    
    
    BuyBottomView* bottomView = [BuyBottomView view];
    self.bottomView = bottomView;
    _tableView.tableFooterView = self.bottomView;
    
    BuyTopImageCell* imageView = [[[NSBundle mainBundle] loadNibNamed:@"BuyTopImageCell" owner:nil options:nil] lastObject];
    self.imageView = imageView;
    _tableView.tableHeaderView = self.imageView;
    
    
    [imageView setClickImageBigBlock:^() {

        // 点击打开大图片
        [self createView];
        
    }];
    
    [_tableView registerNib:[UINib nibWithNibName:@"DetailViewCell" bundle:nil] forCellReuseIdentifier:@"DetailViewCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"BottomCell" bundle:nil] forCellReuseIdentifier:@"BottomCell"];
    
    
}
// 点击打开大图片
-(void)createView{
    
    self.viewBigImage = [[UIView alloc]initWithFrame:self.view.frame];
    self.viewImageBack = [[UIView alloc]initWithFrame:self.view.frame];
    self.viewBigImage.backgroundColor = [UIColor clearColor];
    self.viewImageBack.backgroundColor = [UIColor blackColor];
    self.viewImageBack.alpha = 0;
    
    [self.view addSubview:self.viewBigImage];
    self.rollViewColor = [[JXRollView alloc] initWithFrame:CGRectMake(0, 0, self.viewBigImage.frame.size.width, 277)];
    [self.viewBigImage addSubview:_viewImageBack];
    [self.viewBigImage addSubview:self.rollViewColor];
    
    [UIView animateWithDuration:Time animations:^{
        self.viewImageBack.alpha = 1.0;
        self.rollViewColor.frame = CGRectMake(0, (screenH-self.rollViewColor.frame.size.height)/2, screenW, 277);
    }];
    
    
    [self lunbo];
}

// 打开大图片
-(void)lunbo{
    
    NSString *img1 = @"ad";
    NSString *img2 = @"ae";
    NSString *img3 = @"af";
    arrUrls = [NSArray arrayWithObjects:img1,img2,img3, nil];
    
    self.rollViewColor.pageIndicatorColor = [UIColor lightGrayColor];
    self.rollViewColor.currentPageIndicatorColor = [UIColor redColor];
    self.rollViewColor.delegate = self;
    self.rollViewColor.autoRolling = NO;
    [self.rollViewColor reloadData];
    
    
}
#pragma mark <JXRollViewDelegate>
- (NSInteger)numberOfItemsInRollView:(JXRollView *)rollView {
    return arrUrls.count;
}

- (void)rollView:(JXRollView *)rollView setImageForImageView:(UIImageView *const)imageView atIndex:(NSInteger)index {
    
    
    imageView.image = [UIImage imageNamed:arrUrls[index]];
    self.imgView = imageView;
}

- (void)rollView:(JXRollView *)rollView didTapItemAtIndex:(NSInteger)index {
    NSLog(@"Tap the index %ld", index);
    
    [UIView animateWithDuration:Time animations:^{
        self.viewImageBack.alpha = 0;
        self.rollViewColor.frame = CGRectMake(0, 0, screenW, 277);
    }completion:^(BOOL finished) {
        [self.viewBigImage removeFromSuperview];
    }];
}


//完成拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
    
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.bounds;
    CGSize size = scrollView.contentSize;
    UIEdgeInsets inset = scrollView.contentInset;
    CGFloat currentOffset = offset.y + bounds.size.height - inset.bottom;
    
    CGFloat maximumOffset = size.height;
    //当currentOffset与maximumOffset的值相等时，说明scrollview已经滑到底部了。也可以根据这两个值的差来让他做点其他的什么事情
    
    
    //    NSLog(@"%lf",currentOffset-maximumOffset);
    CGFloat offsetY=currentOffset-maximumOffset;
    
    if (scrollView==_tableView) {
        
        if (offsetY>40) {
            _tableView.bounces=NO;
            _scrollView.pagingEnabled=YES;
            _scrollView.contentSize=CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height*2);
            if (_vc1==nil) {
                _vc1=[[DViewController alloc]init];
                
                __weak ViewController *weakSelf=self;
                _vc1.headRefReshBlock=^()
                {
                    [UIView animateWithDuration:1 animations:^{
                        weakSelf.tableView.frame=CGRectMake(0, 0, weakSelf.view.bounds.size.width, weakSelf.view.bounds.size.height-ViewHeight);
                        weakSelf.vc1.view.frame=CGRectMake(0, weakSelf.view.bounds.size.height, weakSelf.view.bounds.size.width, weakSelf.view.bounds.size.height);
                    }  completion:^(BOOL finished) {
                        weakSelf.scrollView.contentSize=CGSizeMake(weakSelf.view.bounds.size.width, weakSelf.view.bounds.size.height*2);
                    }];
                };
                [self addChildViewController:_vc1];
                _vc1.view.frame=CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height);
                [_scrollView addSubview:_vc1.view];
                
            }
            [UIView animateWithDuration:1 animations:^{
                _tableView.frame=CGRectMake(0, -self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height-ViewHeight);
                _vc1.view.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
            }  completion:^(BOOL finished) {
                _scrollView.contentSize=CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
                _tableView.bounces=NO;
            }];
        }
        else
        {
            _vc1.view.frame=CGRectMake(0, self.view.bounds.size.height-offsetY, self.view.bounds.size.width, self.view.bounds.size.height);
        }
    }
    else
    {
        //        NSLog(@"%f",offset.y);
        if (offset.y<self.view.bounds.size.height) {
            [UIView animateWithDuration:0.5 animations:^{
                _tableView.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-ViewHeight);
                _vc1.view.frame=CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height);
            }  completion:^(BOOL finished) {
                _scrollView.contentSize=CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
                _tableView.bounces=YES;
            }];
        }
    }
}

// 开始滑动时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row==4) {
//        BottomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BottomCell"];
//        return cell;
//    }else{
        DetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailViewCell"];
        return cell;
//    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==4) {
        
        return 60;
    }
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
